package com.yanhui.utils.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class _timeUtils {

    public static void main(String[] args) throws ParseException {
//		BigInteger a = _timeUtil.integerToStamp();
//		System.out.println(a);
        //1487765277962

//		Long a = 1487765277962L;
//		String as = a +"";
//		System.out.println(_timeUtil.stampToDate(as));

        Date d1 = new Date();
        System.out.println(d1);
        d1 = addDate(d1, 1);
        System.out.println(d1);

    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static Date addDate(Date date, int num){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,num);//把日期往后增加一天.整数往后推,负数往前移动 
        date=calendar.getTime();//这个时间就是日期往后推一天的结果 
        return date;
    }

    public static String getBeforeDay(String today, int day) throws Exception{
        SimpleDateFormat sj = new SimpleDateFormat("yyyy-MM-dd");
        Date d = sj.parse(today);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.add(calendar.DATE, day);
        return sj.format(calendar.getTime());
    }

    /**
     * 转为Integer时间戳
     * @return
     * @throws ParseException
     */
    public static Long integerToStamp() throws ParseException{
        Date date = new Date();
        Long res = date.getTime();
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /** 时间格式(MM-dd) */
    public final static String MONTH_PATTERN = "MM-dd";
    /** 时间格式(yyyy-MM-dd) */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    /** 时间格式(yyyyMMddHHmmss) */
    public final static String TEMPLATE_TIME_PATTERN = "yyyyMMddHHmmss";
    /** 时间格式(yyyy-MM-dd-HH-mm-ss) */
    public final static String DATE_TIME_PATTERN2 = "yyyy-MM-dd-HH-mm-ss";
    /** 世界协调时间格式(yyyy-MM-ddTHH:mm:ss+08:00) */
    public final static String DATE_TIME_PATTERN3 = "yyyy-MM-dd'T'HH:mm:ss+08:00";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    public static Date parseStr(String dateStr, String pattern){
        SimpleDateFormat sdf=new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date parseDate(String dateStr, String pattern) throws ParseException{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = simpleDateFormat.parse(dateStr);
        return date;
    }

    /**
     * 两时间差
     * @param d1
     * @param d2
     * @return
     */
    public static String getDiffTime(Date d1, Date d2){
        String result="";
        long diff = d2.getTime() - d1.getTime();
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
        long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
        long seconeds = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60)-minutes*(1000* 60))/(1000);
        if(days != 0){
            result = days+"天"+hours+"小时"+minutes+"分"+seconeds+"秒";
        }if(days==0 && hours != 0){
            result = hours+"小时"+minutes+"分"+seconeds+"秒";
        }if(days==0 && hours==0 && minutes != 0){
            result = minutes+"分"+seconeds+"秒";
        }if(days==0 && hours==0 && minutes==0 && seconeds != 0){
            result = seconeds+"秒";
        }
        return ""+result;
    }



}
